/*
  Copyright © 2010  Vinícius dos Santos Oliveira
  Copyright © 2010  Felipe Carlos Lima dos Santos

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#include <QGraphicsEffect>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsColorizeEffect>
#include "pushbuttonitem.h"
#include <QDebug>

static QSvgRenderer bgOff(QString(":/gfx/svg/scribble_button_bg_off.svg"));
static QSvgRenderer bgOn(QString(":/gfx/svg/scribble_button_bg_on.svg"));

void PushButtonItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    event->accept();
    emit pressed();
    pressedAt = QGraphicsObject::sceneBoundingRect();
    if(m_hasBg){
        mustHighlight = true;
        update();
    }else{
        QGraphicsColorizeEffect * effect = new QGraphicsColorizeEffect(this);
        effect->setColor(QColor(0xFF, 0xFF, 0xFF, 0x7F));
        effect->setStrength(.5);
        QGraphicsItem::setGraphicsEffect(effect);
        QGraphicsItem::setOpacity(.875);
    }
}

void PushButtonItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    emit released();
    if(pressedAt.contains(event->scenePos())){
        if(checkable){
            emit toggled(checked = !checked);
        }
        emit clicked(this);
    }
    if(checked){
        mustHighlight = true;
        if(!m_hasBg){
            QGraphicsColorizeEffect * effect = new QGraphicsColorizeEffect(this);
            effect->setColor(QColor(0x3F, 0x3F, 0x3F, 0x7F));
            effect->setStrength(.75);
            setGraphicsEffect(effect);
        }
    }else{
        setGraphicsEffect(NULL);
        mustHighlight = false;
        update();
    }
    QGraphicsItem::setOpacity(1.);
}

void PushButtonItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(QGraphicsItem::flags() & QGraphicsItem::ItemIsMovable){
        QGraphicsObject::setPos(QGraphicsObject::pos() + (1. / QGraphicsObject::scale()) *
                                 (event->pos() - event->lastPos()));
    }
}

PushButtonItem::PushButtonItem(const QString &fileName, QGraphicsItem *parentItem) :
        QGraphicsObject(parentItem),
        image(fileName),
        checkable(false),
        checked(false),
        m_hasBg(false),
        mustHighlight(false)
{
}

bool PushButtonItem::isCheckable() const
{
    return checkable;
}

void PushButtonItem::setCheckable(bool checkable)
{
    this->checkable = checkable;
    if(!checkable){
        if(checked){
            checked = false;
            emit toggled(false);
            setGraphicsEffect(NULL);
            mustHighlight = false;
            update();
        }
    }
}

bool PushButtonItem::isChecked() const
{
    return checked;
}

void PushButtonItem::setHasBg(bool hasBg)
{
    this->m_hasBg = hasBg;
    update();
}

bool PushButtonItem::hasBg() const
{
    return m_hasBg;
}

void PushButtonItem::setChecked(bool checked)
{
    if(this->checked != checked){
        emit toggled(this->checked = checked);
    }
    if(checked){
        if(!m_hasBg && !graphicsEffect()){
            QGraphicsColorizeEffect * effect = new QGraphicsColorizeEffect(this);
            effect->setColor(QColor(0x3F, 0x3F, 0x3F, 0x7F));
            effect->setStrength(.75);
            setGraphicsEffect(effect);
        }

        mustHighlight = true;
        update();
    }else{
        setGraphicsEffect(NULL);
        mustHighlight = false;
        update();
    }
}

void PushButtonItem::toggle()
{
    if(checked){
        setChecked(false);
    }else{
        setChecked(true);
    }
}

QRectF PushButtonItem::boundingRect() const
{
    QSize size;

    if(m_hasBg) {
       size = bgOff.defaultSize();
    } else {
        size = image.defaultSize();
    }

    return QRectF(0, 0, size.width(), size.height());
}

void PushButtonItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if(m_hasBg) {
        if(!mustHighlight) {
            bgOff.render(painter, boundingRect());
        } else {
            bgOn.render(painter, boundingRect());
        }
    }

    QSize size = image.defaultSize();
    image.render(painter, QRectF(boundingRect().width() / 2 - size.width() / 2,
                                 boundingRect().height() / 2 - size.height() / 2,
                                 size.width(),
                                 size.height()));
}
